﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NLog;
using OddUserManager.Model;
using OddUserManager.Service.IServices;
using OddUserManager.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace OddUserManager.Web.Controllers
{
    
    public class RoleController: BaseController
    {
        static Logger log = LogManager.GetCurrentClassLogger();
        private readonly IRoleService _service;
        public RoleController(IRoleService roleService)
        {
            _service = roleService;
        }
        // GET: Menus
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新增或编辑功能
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Edit(Role dto)
        {
            JsonData data = new JsonData();
            try
            {
                if (dto.Id <= 0)
                    dto.CreateTime = DateTime.Now;
                var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                if (result.Succeeded)
                {
                    var Id = result.Principal.Identity.Name;
                    dto.CreateUserId = int.Parse(Id);
                    _service.InsertOrUpdateRole(dto);
                    data.Message = "更新成功";
                }
            }
            catch (DataMisalignedException ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
             return Json(data);
        }

        public IActionResult GetAllPageList(int startPage, int pageSize)
        {

            JsonData data = new JsonData();
            try
            {
                int rowCount = 0;
                var result = _service.GetAllPageList(startPage, pageSize, out rowCount);
                data.Data = new PageCollection()
                {
                    AllCount = rowCount,
                    PageCount = Math.Ceiling(Convert.ToDecimal(rowCount) / pageSize),
                    Data = result,
                    PageIndex=startPage,
                    PageSize=pageSize
                };
            }
            catch (Exception ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }
        public IActionResult DeleteMuti(string ids)
        {
            JsonData data = new JsonData();
            try
            {
                string[] idArray = ids.Split(',');
                List<int> delIds = new List<int>();
                foreach (string id in idArray)
                {
                    delIds.Add(int.Parse(id));
                }
                _service.DeleteBatchRole(delIds);
                data.Message = "删除成功";
            }
            catch (DataMisalignedException ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }
        public IActionResult Delete(int id)
        {
            JsonData data = new JsonData();
            try
            {
                _service.DeleteRoleById(id);
                
            }
            catch (DataMisalignedException ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }
        public IActionResult Get(int id)
        {
            JsonData data = new JsonData();
            try { 
                data.Data = _service.GetRoleById(id);
            }
            catch (DataMisalignedException ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }

        /// <summary>
        /// 根据角色获取权限
        /// </summary>
        /// <returns></returns>
        public IActionResult GetMenusByRole(int roleId)
        {
            JsonData data = new JsonData();
            try
            {
                data.Data = _service.GetAllMenuListByRole(roleId);
            }
            catch (DataMisalignedException ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }

        public IActionResult SavePermission(int roleId, List<RoleMenus> roleMenus)
        {
            JsonData data = new JsonData();
            try
            {
                data.Success = _service.UpdateRoleMenu(roleId, roleMenus);
            }
            catch (DataMisalignedException ex)
            {
                data.Success = false;
                data.Message = ex.Message;
                log.Error(ex, ex.Message);
            }
            return Json(data);
        }
    }
}
